<?php
namespace app\agent\controller;
use app\agent\controller\BaseController;
use think\Db;


class FinaceController  extends BaseController {

	public function money($begin_time='',$end_time='') {
		if(!empty($begin_time) && !empty($end_time)){
				$where['add_time'] = ['between time',[input('begin_time'),input('end_time')]];
		}
		$this->assign('begin_time',$begin_time);
		$this->assign('end_time',$end_time);
		
		$where['agent_id'] = session('agent_id');
		$list = db('agent_charge')
					->where($where)
					->order('add_time desc')
					->paginate(15);
		$this->assign('list',$list);
		$this->assign('empty','<span class="empty" >没有数据</span>');
		return $this->fetch();
	}
	public function tongji() {
		$where['agent_id'] = session('agent_id');
		$where['order_type']=["in",[1,3]];
		$c1=db('agent_charge')->where($where)->sum('balance');
		$c1_count=db('agent_charge')->where($where)->count();
		//收款金额
		$this->assign("c1", $c1);
		$this->assign("c11",$c1_count);
		
		//今日
		$this->assign("c4", db('agent_charge')->whereTime('add_time', 'today')->where($where)->sum('balance'));
		$this->assign("c41", db('agent_charge')->whereTime('add_time', 'today')->where($where)->count());
		//昨天
		$this->assign("c5", db('agent_charge')->whereTime('add_time', 'today')->where($where)->sum('balance'));
		$this->assign("c51", db('agent_charge')->whereTime('add_time', 'today')->where($where)->count());
		//本周
		$this->assign("c6", db('agent_charge')->whereTime('add_time', 'today')->where($where)->sum('balance'));
		$this->assign("c61", db('agent_charge')->whereTime('add_time', 'today')->where($where)->count());
		//本月
		$this->assign("c7", db('agent_charge')->whereTime('add_time', 'today')->where($where)->sum('balance'));
		$this->assign("c71", db('agent_charge')->whereTime('add_time', 'today')->where($where)->count());
		
				//体现
		$where['order_type']=5;
		$this->assign("c2", db('agent_charge')->where($where)->sum('balance'));
		$this->assign("c21", db('agent_charge')->where($where)->count());
		//退款金额
		$where['order_type']=2;
		$this->assign("c3", db('agent_charge')->where($where)->sum('balance'));
		$this->assign("c31", db('agent_charge')->where($where)->count());
		
		return $this->fetch();
	}
	public function tixian() {
		$list = db('agent_tixian')->where('agent_id',session('agent_id'))->order('add_time desc')->paginate(10);
    	$this->assign('list',$list);
		$this->assign('empty','<span class="empty" >没有数据</span>');
		$where['id'] = session('agent_id');
		$this->assign('data',select_data('agent',$where));
		return $this->fetch();
	}
	
	public function tixian_add() {
		
		$where['id'] = session('agent_id');
		$agent_info = select_data('agent',$where);
		$this->assign('data',$agent_info);
		
		if(request()->isPost()){
    		
			$result = true;
    		// 启动事务
			Db::startTrans();			
			//oem累计收入
			$agent_total_money = db('agent_charge')
										->where('agent_id',$agent_info['id'])
										->where('order_type','in','1,2')
										->sum('balance');
			//检查累计收入 
			if(bccomp($agent_total_money, $agent_info['total_income'], 2) != 0){
				return json(['code'=>401,'msg'=>'数据异常,提现申请失败']);
			}
			//计算agent余额 = 累计收入 - 提现金额
			$agent_cut_money = bcsub($agent_total_money, $agent_info['total_withdrawals_money'], 4);
			//检查agent余额 
			if(bccomp($agent_cut_money, $agent_info['balance'], 2) != 0){
				return json(['code'=>402,'msg'=>'数据异常,提现申请失败']);
			}
			//检查提现金额，核对余额是否合法
			if(bccomp($agent_info['balance'], $_POST['money'], 2)>=0){
				
				//计算手续费
				$sxf_money = 0;
				
				if($_POST['withdraw_type'] == 1){
					$sxf_money = bcmul($_POST['money'], 0.0001, 4);
				}elseif($_POST['withdraw_type'] == 2){
					$sxf_money = bcmul($_POST['money'], 0.0002, 4);
				}
				
				if(bccomp($sxf_money, 0.01, 4) == -1){
					$sxf_money = 0.01;
				}else{
					$sxf_money = bcmul($_POST['money'], 0.0002, 4);
				}
				//计算实际到账
				$dz_money = bcsub($_POST['money'], $sxf_money, 2);
				
				//计算当前余额
				$cut_balance = bcsub($agent_info['balance'], $_POST['money'], 2);
				//更新agent信息
				$rest1 = db('agent')->where('id', $agent_info['id'])->update(['balance'=>$cut_balance]);
				if(!$rest1)$result =false;
				
				//agent提现，余额变化日志
				$rest2 = add_agent_charge($agent_info['id'],-$_POST['money'],5,0,"商家余额提现",2);
				if(!$rest2)$result =false;
				
				//提现日志
				$rest3 = add_agent_tixan($agent_info['id'],$_POST['money'],$_POST['withdraw_type'],$sxf_money,$dz_money);
				if(!$rest3)$result =false;
				
				if($result){
					// 提交事务
    				Db::commit();
					
					//短信提醒
					
					return json(['code'=>200,'msg'=>'申请成功']);
				}else{
					//回滚
		    		Db::rollback();
					return json(['code'=>403,'msg'=>'数据异常,提现申请失败']);
				}
			}else{
					return json(['code'=>405,'msg'=>'数据异常,提现申请失败']);
			}
    	}
		return $this->fetch();
	}
	
	
}
